﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Linq;

namespace MII_FIF.Layouts.MII_FIF
{
    public partial class Manage : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.ManagePDGroupName))
            {
                ((Label)Header.FindControl("lblHeader")).Text = "Manage Physical Data";
                if (!IsPostBack)
                {
                    Initialize();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void Initialize()
        {
            #region BindDDL
            InventoryManagement.Rack_Number rackNumber = new InventoryManagement.Rack_Number();
            rackNumber.IsDeleted = false;
            DataTable dtRackNumber = rackNumber.SelectByParam();
            Generic.BindDDL(ddlRackNumber, dtRackNumber, InventoryManagement.Rack_Number_Fields.Title.ToString(), InventoryManagement.Rack_Number_Fields.ID.ToString());
            #endregion
        }

        void UpdatePhysicalData()
        {
            if (!ddlRackNumber.SelectedValue.Equals("-1"))
            {
                DataTable datatable = new DataTable();
                datatable.Columns.Add("RackSlot");
                datatable.Columns.Add("LabelName");
                for (int i = 1; i <= 40; i++)
                {
                    DataRow row = datatable.NewRow();
                    row["RackSlot"] = i;
                    row["LabelName"] = InventoryManagement.Inventory_Physical.CheckRackFilled(i, ddlRackNumber.SelectedValue);
                    datatable.Rows.Add(row);
                }

                GridDataTable.DataSource = datatable;
                ViewState["datatable"] = datatable;
                GridDataTable.DataBind();
                ddlRackNumber.Focus();
            }
        }

        protected void ddlRackNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdatePhysicalData();
        }

        protected void GridDataTable_DataBound(object sender, EventArgs e)
        {
            bool changeColor = false;
            for (int i = GridDataTable.Rows.Count - 1 ; i >= 0; i--)
            {                
                GridViewRow row = GridDataTable.Rows[i];
                GridViewRow previousRow = (i==0) ? GridDataTable.Rows[i] : GridDataTable.Rows[i - 1];

                if (!row.Cells[1].Text.Equals("&nbsp;"))
                {
                    if (changeColor)
                    {
                        row.BackColor = System.Drawing.Color.LightSkyBlue;
                    }
                    else
                    {
                        row.BackColor = System.Drawing.Color.DarkCyan;
                    }
                }

                if (row.Cells[1].Text == previousRow.Cells[1].Text)
                {                    
                    if (previousRow.Cells[1].RowSpan == 0)
                    {
                        if (row.Cells[1].RowSpan == 0)
                        {
                            previousRow.Cells[1].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[1].RowSpan = row.Cells[1].RowSpan + 1;
                        }
                        row.Cells[1].Visible = false;   
                    }
                }
                else
                {
                    if (!row.Cells[1].Text.Equals("&nbsp;"))
                    {                        
                        changeColor = !changeColor;
                    }
                }
            }
        }
    }
}
